home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 June / Macworld (1998-06).dmg / Shareware World / Info / For Developers / MacsBug 6.5.4a4 / Building dcmds / C Samples / Drvr.c next >
Text File  |  1998-02-11  |  7KB  |  326 lines

  1. /*
  2.     File:        Drvr.c
  3.  
  4.     Contains:    This is the Unit Table dcmd.
  5.  
  6.     Written by:    JM3 = Jim Murphy
  7.                 DAL = Dave Lyons
  8.                 sad = Scott Douglass
  9.  
  10.     Copyright:    © 1988,1993-1997 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.     File Ownership:
  13.  
  14.         DRI:                Jim Murphy
  15.  
  16.         Other Contact:        Dave Lyons
  17.  
  18.         Technology:            MacsBug
  19.  
  20.     Writers:
  21.  
  22.         (DAL)    Dave Lyons
  23.  
  24.     Change History (most recent first):
  25.  
  26.          <7>     6/17/97    DAL        -b = busy only. Version 3.0.1.
  27.          <6>   25-Jan-96    JM3        Updated the sample build commands to be current.
  28.          <5>    10/27/95    DAL        Draw "-nil-" instead of empty-string for nil DRVR handles
  29.                                     (merged in from Scott Douglass' source).
  30.          <4>   10-Dec-94    JM3        Updated for new format 3 dcmd requirements.
  31.          <3>     3/14/94    DAL        Re-layed-out the columns: removed Window and made room for
  32.                                     everything to be nice on 13" monitors. Made the "that's strange"
  33.                                     message not show up for dRef = 0xFFFE (.Sony for HD-20). Now
  34.                                     Hoon is happy.
  35.          <2>     9/14/93    DAL        made the driver name column wider
  36.  
  37.     Modification history:
  38.          7Dec88 sad        show driver version
  39.         29Nov88 sad        revised for new dcmd names
  40.         13Oct88 sad        written from file.c
  41.  
  42.     The following MPW commands will build the dcmd and copy it to the "Debugger Prefs" file
  43.     in the System folder. The dcmd's name in MacsBug will be the name of the file built by
  44.     the Linker.
  45.  
  46.     C Drvr.c
  47.     Link -o Drvr -sg Main=STDCLIB,STDIO,SANELIB dcmdGlue.a.o Put.c.o Drvr.c.o"    ∂
  48.         "{Libraries}Runtime.o"
  49.     BuildDcmd Drvr 193 -format3
  50.     Echo 'include "Drvr";'    |    Rez -a -o "{SystemFolder}Debugger Prefs"
  51.  
  52. */
  53.  
  54. //#ifdef USESTDIO
  55. //    #include <stdio.h>
  56. //#endif
  57.  
  58. #include <Types.h>
  59. #include <Memory.h>
  60. #include <Devices.h>
  61. #include <SysEqu.h>
  62.  
  63. #include "dcmd.h"
  64. #include "put.h"
  65.  
  66.  
  67. typedef struct DRVR
  68. {
  69.     short drvrFlags;
  70.     short drvrDelay;
  71.     short drvrEMask;
  72.     short drvrMenu;
  73.     short drvrOpen;
  74.     short drvrPrime;
  75.     short drvrCtl;
  76.     short drvrStatus;
  77.     short drvrClose;
  78.     Str255 drvrName;
  79. } DRVR;
  80.  
  81.  
  82. static void DrawHdr()
  83. {
  84. //                    0         1         2         3         4         5         6         7         8         9
  85. //                    0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
  86.     dcmdDrawLine("\pdRef dNum Driver                      Flg  Ver   qHead   Storage Dely  Drvr at DCE at");
  87. }
  88.  
  89. #define kColumnNameEnd         37
  90. #define kColumnVersionEnd    46
  91. #define kColumnQHeadEnd        54
  92. #define kColumnStorageEnd    64
  93. #define kColumnDriverAtEnd    78
  94. #define kColumnDCEAtEnd        85
  95.  
  96.  
  97. static void DrawDCE(int dref, AuxDCE* dcep)
  98. {
  99.     DRVR*    drvrp;
  100.  
  101.     PutUHexWord(dref);
  102.     PutSpace();
  103.     PutUHexWord(~dref);
  104.     PutSpace();
  105.     if (dcep->dCtlFlags & 0x40)
  106.     {
  107.         if (dcep->dCtlDriver)
  108.         {
  109.             drvrp = * (DRVR **) dcep->dCtlDriver;
  110.             if (drvrp)
  111.                 PutPStrTruncTo(drvrp->drvrName, kColumnNameEnd);
  112.             else
  113.                 PutPStrTruncTo("\p-purged-", kColumnNameEnd);
  114.         }
  115.         else
  116.         {
  117.             PutPStrTruncTo("\p-nil-", kColumnNameEnd);
  118.         };
  119.     }
  120.     else
  121.     {
  122.         if (dcep->dCtlDriver)
  123.         {
  124.             drvrp = (DRVR*)dcep->dCtlDriver;
  125.             PutPStrTruncTo(drvrp->drvrName, kColumnNameEnd);
  126.         }
  127.         else
  128.         {
  129.             PutPStrTruncTo("\p-nil-", kColumnNameEnd);
  130.         };
  131.     };
  132.     PutSpace();
  133.     PutChar((dcep->dCtlFlags & 0x80) ? 'B' : 'b');
  134.     PutChar((dcep->dCtlFlags & 0x40) ? 'H' : 'P');
  135.     PutChar((dcep->dCtlFlags & 0x20) ? 'O' : 'C');
  136.     PutSpace();
  137.     PutUDecTo((unsigned char) dcep->dCtlQHdr.qFlags,kColumnVersionEnd);            // version
  138.     PutSpace();
  139.     PutUHexZTo((unsigned long) dcep->dCtlQHdr.qHead,8,kColumnQHeadEnd);            // qHead
  140.     PutSpace();
  141.     PutUHexZTo((unsigned long) dcep->dCtlStorage,8,kColumnStorageEnd);            // Storage
  142.     PutSpace();
  143.     PutUHexWord((unsigned long) dcep->dCtlDelay);
  144.     PutSpace();
  145.     PutUHexZTo((unsigned long) drvrp,8,kColumnDriverAtEnd);
  146.     PutSpace();
  147.     PutUHexZTo((unsigned long) dcep,8,kColumnDCEAtEnd);
  148.     PutLine();
  149.  
  150.     // For drvr FFFE (.Sony for the HD20, pre-SCSI), it's okay that
  151.     // the dCtlRefNum is $FFFB (don't whine about it)
  152.  
  153.     if ( (dref != dcep->dCtlRefNum) && ( ((unsigned short) dref) != (unsigned short) 0xFFFE) )
  154.     {
  155.         PutPStr("\p   that is strange:  dCtlRefNum = ");
  156.         PutUHexWord(dcep->dCtlRefNum);
  157.         PutLine();
  158.     }
  159. } // DrawDCE
  160.  
  161.  
  162. pascal void DoDrvrCommand(dcmdBlock* paramPtr)
  163. {
  164.     Boolean doOne = false;
  165.     Boolean busyOnly = false;
  166.     long dref;
  167.     int NumDCEs;
  168.  
  169.     dcmdSwapWorlds();
  170.  
  171.     dcmdDrawLine("\pDisplaying Driver Control Entries");
  172.  
  173.     // Get low-memory values after dcmdSwapWorlds().
  174.  
  175.     NumDCEs = *(unsigned short *)UnitNtryCnt;
  176.  
  177.     if ( dcmdPeekAtNextChar() == '-' )
  178.     {
  179.         short oldPos = dcmdGetPosition();
  180.         short ch;
  181.  
  182.         dcmdGetNextChar();        // eat the "-"
  183.         
  184.         ch = dcmdGetNextChar();
  185.  
  186.         if ( ch == 'b' || ch == 'B' )
  187.             busyOnly = true;
  188.         else
  189.             dcmdSetPosition( oldPos );
  190.     }
  191.  
  192.     if ( !busyOnly )
  193.         (void) dcmdGetNextExpression(&dref, &doOne);
  194.  
  195.     if (doOne)    // info on one specific driver
  196.     {
  197.         int dnum;
  198.         dref = (short) dref;
  199.         if (dref < 0)
  200.         {
  201.             dnum = ~dref;
  202.         }
  203.         else
  204.         {
  205.             dnum = dref;
  206.             dref = ~dref;
  207.         }
  208.  
  209.         if (dnum > NumDCEs)
  210.         {
  211. //#ifdef USESTDIO
  212. //            Str255 line;
  213. //            sprintf(line,"Bad refnum 0x%.4x",(unsigned short)dref);
  214. //            dcmdDrawLine((Str255)c2pstr(&line));
  215. //#else
  216.             PutPStr("\pBad refnum ");
  217.             PutUHexWord(dref);
  218.             PutSpace();
  219.             PutUHexWord(~dref);
  220.             PutLine();
  221. //#endif
  222.         }
  223.         else
  224.         {
  225.             AuxDCE** dceh = (*(AuxDCE****)UTableBase)[dnum];
  226.             if (dceh)
  227.             {
  228.                 DrawHdr();
  229.                 DrawDCE(dref,*dceh);
  230.             }
  231.             else
  232.             {
  233.                 PutPStr("\pDriver ");
  234.                 PutUHexWord(dref);
  235.                 PutSpace();
  236.                 PutUHexWord(~dref);
  237.                 PutPStr("\p is not in installed");
  238.                 PutLine();
  239.             }
  240.         }
  241.     }
  242.     else        // no driver number specified on command line
  243.     {
  244.         int DCEsUsed = 0;
  245.         int dnum;
  246.         AuxDCE*** dcehp;
  247.         Boolean foundOne = false;
  248.         for(dnum = 0, dcehp = *(AuxDCE****)UTableBase; dnum < NumDCEs; dnum++, dcehp++)
  249.         {
  250.             if (*dcehp)
  251.             {
  252.                 if (busyOnly && (**dcehp)->dCtlQHdr.qHead == 0)
  253.                     continue;
  254.  
  255.                 DCEsUsed++;
  256.                 if (!foundOne)
  257.                 {
  258.                     DrawHdr();
  259.                     foundOne = true;
  260.                 }
  261.                 DrawDCE(~dnum,**dcehp);
  262.             }
  263.  
  264.             if (paramPtr->aborted)
  265.                 break;
  266.         }
  267.  
  268.         if ( !paramPtr->aborted )
  269.         {
  270.             if ( busyOnly )
  271.             {
  272.                 if ( !foundOne )
  273.                 {
  274.                     PutPStr("\pNo drivers are busy.");
  275.                     PutLine();
  276.                 }
  277.             }
  278.             else
  279.             {
  280.                 PutUDec(NumDCEs);
  281.                 PutPStr("\p Unit Table entries, ");
  282.                 PutUDec(DCEsUsed);
  283.                 PutPStr("\p in use, ");
  284.                 PutUDec(NumDCEs - DCEsUsed);
  285.                 PutPStr("\p free");
  286.                 PutLine();
  287.             }
  288.         }
  289.     }    
  290.  
  291.     dcmdSwapWorlds();
  292. } // DoDrvrCommand
  293.  
  294.  
  295. pascal void CommandEntry(dcmdBlock* paramPtr)
  296. {
  297.  
  298.     static const Str255 usageStr = "\p[refNum|num] | -b";
  299.  
  300.     switch (paramPtr->request)
  301.     {
  302.         case dcmdInit:
  303.             break;
  304.  
  305.         case dcmdHelp:
  306.             dcmdDrawLine("\pDisplays driver information for the given refNum or all installed");
  307.             dcmdDrawLine("\pdrivers. Flags are B/b=Busy, H/P=Handle/Ptr, O/C=Open/Closed.");
  308.             dcmdDrawLine("\p  drvr -b  shows only the drivers that are busy.");
  309.             break;
  310.  
  311.         case dcmdGetInfo:
  312.             * (long *) &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->dcmdVersion = 0x03018000; // version 3.0.1 final
  313.             BlockMoveData(&usageStr, &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->usageStr, usageStr[0]+1);
  314.             break;
  315.  
  316.         case dcmdDoIt:
  317.             DoDrvrCommand(paramPtr);
  318.             break;
  319.  
  320.         // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
  321.  
  322.         default:
  323.             break;
  324.     }
  325. } // CommandEntry
  326.